/*
 * PlayerImpl.h
 *
 *  Created on: 2011/05/28
 *      Author: naoki
 */

#ifndef INCLUDED_NTENGINE_SOUND_MANAGERIMPL_H
#define INCLUDED_NTENGINE_SOUND_MANAGERIMPL_H

#include "NTEngine/NTEngine.h"
#include "NTEngine/Sound/Manager.h"

#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>

namespace NTEngine {
namespace Sound {

class ManagerImpl {
public:
	ManagerImpl()
	: mSL( 0 )
	, mEngine( 0 )
	, mOutputMix( 0 ) {
		SLresult result;
		// create engine
		const SLEngineOption options[] = {
			SL_ENGINEOPTION_THREADSAFE, SL_BOOLEAN_TRUE
		};
		result = slCreateEngine( &mSL, 1, options, 0, 0, 0 );
		ASSERT( SL_RESULT_SUCCESS == result );
		result = ( *mSL )->Realize( mSL, SL_BOOLEAN_FALSE );
		ASSERT( SL_RESULT_SUCCESS == result );
		result = ( *mSL )->GetInterface( mSL, SL_IID_ENGINE, &mEngine );
		ASSERT( SL_RESULT_SUCCESS == result );

		// create output mix
		result = ( *mEngine )->CreateOutputMix( mEngine, &mOutputMix, 0, 0, 0 );
		ASSERT( SL_RESULT_SUCCESS == result );
		result = ( *mOutputMix )->Realize( mOutputMix, SL_BOOLEAN_FALSE );
		ASSERT( SL_RESULT_SUCCESS == result );
	}

	~ManagerImpl() {
		// destroy output mix object, and invalidate all associated interfaces
		if ( mOutputMix ) {
			( *mOutputMix )->Destroy( mOutputMix );
			mOutputMix = 0;
		}
		// destroy engine object, and invalidate all associated interfaces
		if ( mSL ) {
			( *mSL )->Destroy( mSL );
			mSL = 0;
			mEngine = 0;
		}
	}

	SLObjectItf mSL; /// SLインターフェース
	SLEngineItf mEngine; /// エンジンインターフェース
	SLObjectItf mOutputMix; /// 出力インターフェース
};

extern ManagerImpl* gManagerImpl;

} // namespace Sound
} // namespace NTEngine

#endif // INCLUDED_NTENGINE_SOUND_MANAGERIMPL_H
